package nc.nc_xiuxian;

import org.bukkit.BanList;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.attribute.Attribute;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.*;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

import static nc.nc_xiuxian.JINGJIE.playerlistload;
import static nc.nc_xiuxian.NC_XIUXIAN.plugin;
import static nc.nc_xiuxian.NC_XIUXIAN.ywxs;

//属性读取事件，在玩家加入游戏时读取对应的属性和境界
//玩家退出游戏时的属性以及境界存储
//精神力以及精气恢复

public class LISTENER_FILELOAD implements Listener {
    static HashMap<Player,Integer>xxjs=new HashMap<>();
    public static void usejq(Player player) throws IOException { //精气消耗
        if(SHUXING.sxloadapi(player,"jq")<800&&SHUXING.sxloadapi(player,"jq")>500){
            SHUXING.sxsetadd(player,"jq",-0.1);
        }
        else if(SHUXING.sxloadapi(player,"jq")<300&&SHUXING.sxloadapi(player,"jq")>0){
            SHUXING.sxsetadd(player,"jq",-0.1);
        }
        else if(SHUXING.sxloadapi(player,"jq")<=0){
            if(xxjs.get(player)==null){
                int a = 1;
                xxjs.put(player,a);
                player.sendMessage("你太累了，需要休息了");
            }
            else{
                int a = xxjs.get(player);
                a++;
                if(a>=50){
                    a=0;
                    player.sendMessage("你太累了，需要休息了");
                }
                xxjs.put(player,a);
            }
        }
        else {
            SHUXING.sxsetadd(player,"jq",-0.1);
        }
    }
    static HashMap<Player,Location>playerLocation=new HashMap<>();
    @EventHandler
    public static void playermoveevent(PlayerMoveEvent event) throws IOException { //移动消耗精气
        Player player = event.getPlayer();
        Location oldlocation = playerLocation.get(player);
        Location location1 = player.getLocation();
        location1=new Location(location1.getWorld(),(int)location1.getX(),(int)location1.getY(),(int)location1.getZ());
        if(oldlocation!=location1){ //判断是否不同来扣除精力
            playerLocation.put(player,location1);
            usejq(player);

            BukkitTask bukkitTask=playerjshf.get(player.getName());
            if(bukkitTask!=null){
                bukkitTask.cancel();
                playerjshf.remove(player.getName());
            }
        }

    }
    @EventHandler
    public static void playerjoinevent(PlayerJoinEvent event) throws IOException {
        Player player = event.getPlayer();
        JINGJIE.playerlistadd(player); // 玩家加入游戏，添加玩家列表
    }
    @EventHandler
    public static void playerqevent(PlayerQuitEvent event) throws IOException {
        Player player = event.getPlayer();
        SHUXING.sxupdata(player.getName());
    }
    static HashMap<String,BukkitTask>playerjshf=new HashMap<>();
    @EventHandler
    public static void playersleepevent(PlayerBedEnterEvent event){
        Player player = event.getPlayer();
        Location location = player.getBedSpawnLocation();
        int bedx= (int) location.getX();
        int bedy= (int) location.getY();
        int bedz= (int) location.getZ();
        location = new Location(location.getWorld(), bedx,bedy,bedz);
        Location playerlocation = new Location(player.getWorld(),(int)ywxs(player.getLocation().getX()),(int)ywxs(player.getLocation().getY()),(int)ywxs(player.getLocation().getZ()));
        //player.sendMessage(location+"\n"+playerlocation);
        if(playerlocation.equals(location)){
            BukkitRunnable runnable = new BukkitRunnable() {
                @Override
                public void run() {
                    if(SHUXING.sxloadapi(player,"jq")<SHUXING.sxloadapi(player,"maxjq")){
                        try {
                            SHUXING.sxsetadd(player,"jq",100.0);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    if(SHUXING.sxloadapi(player,"js")<SHUXING.sxloadapi(player,"maxjs")){
                        try {
                            SHUXING.sxsetadd(player,"js",10.0);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    player.sendMessage("你躺在床上感觉一阵放松,精神力和精气都再恢复");
                }
            };
            BukkitTask task = runnable.runTaskTimer(plugin,0L,60L);
            playerjshf.put(player.getName(),task);
        }
    }
    @EventHandler
    public static void playerupsleepevent(PlayerBedLeaveEvent event){
        Player player = event.getPlayer();
        if(playerjshf.get(player.getName())!=null){
            BukkitTask task = playerjshf.get(player.getName());
            task.cancel();
            playerjshf.remove(player.getName());
        }
    }
    @EventHandler
    public static void playerdeathevent(PlayerDeathEvent event){
        Player player = event.getEntity();

    }
}
